package com.rst.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rst.dao.IndexDAO;
import com.rst.model.Country;
import com.rst.model.Currency;
import com.rst.model.InstituteDetail;
import com.rst.model.StudentCategory;
import com.rst.model.TimeZone;

@Service
public class IndexService {

	@Autowired
	private IndexDAO indexDAO ;

	public InstituteDetail getiInstituteDetails() {
		
		return null;
	}

	public InstituteDetail getInstituteDetailById(Long instituteId) {
		return indexDAO.getInstituteDetailById(instituteId);
	}

	public void update(InstituteDetail newDetail, Long instituteId) {
		newDetail.setInstituteId(instituteId);
		indexDAO.udateInstituteDetail(newDetail);		
	}

	private void updateDetails(InstituteDetail oldDetail,InstituteDetail newDetail) {
		oldDetail.setInstituteId(newDetail.getInstituteId());
/*		oldDetail.setName(newDetail.getName());
		oldDetail.setAddress(newDetail.getAddress());
		oldDetail.setPhoneNo(newDetail.getPhoneNo());
		oldDetail.setEmail(newDetail.getEmail());
		oldDetail.setLogoUrl(newDetail.getLogoUrl());
		oldDetail.setCurrencyId(newDetail.getCurrencyId());
		oldDetail.setCityId(newDetail.getCityId());
		oldDetail.setTimeZoneId(newDetail.getTimeZoneId());
		oldDetail.setUpdatedAt(new Date());
*/	}

	public Long save(InstituteDetail detail) {
		
		return indexDAO.saveInstituteDetail(detail);		
	}
 
	public List<TimeZone> getTimeZoneList() {
		return indexDAO.getTimeZones();
	}

	public List<StudentCategory> getStudentCategory() {
		return indexDAO.getStudentCategory();
	}

	public List<Currency> getCurrenciesList() {
		return indexDAO.getCurrenciesList();
	}

	public Long update(InstituteDetail detail) {
		return indexDAO.udateInstituteDetail(detail);		
	}
	public String getCityNameList(String name) {
		StringBuilder cityNamesStr = new StringBuilder();
		List<Object[]> cityNames = null;
		cityNames = indexDAO.getCityNameList(name);

		if(cityNames != null && cityNames.size() > 0) {
			for(Object[] obj : cityNames) {
				if(obj != null && obj[0] != null && obj[1] != null && obj[2] != null && obj[3] != null) {
					if(obj[2].toString().equalsIgnoreCase(obj[1].toString())){
						cityNamesStr.append(obj[1]).append("(").append(obj[3]).append(")").append("|").append(obj[0]).append("|").append(obj[1]).append("|").append(obj[2]).append("|").append(obj[3]).append("\n");
					}else{
						cityNamesStr.append(obj[1]).append("(").append(obj[2]).append(", ").append(obj[3]).append(")").append("|").append(obj[0]).append("|").append(obj[1]).append("|").append(obj[2]).append("|").append(obj[3]).append("\n");
					}
				}
			}
		}
		String str = "";
		if(cityNamesStr.length() > 0) {
			str = cityNamesStr.substring(0, cityNamesStr.length() - 1);
		}
		return str;		
	}

	public List<Country> getCountryList() {
		return indexDAO.getCountryList();	
	}

	
}
